package ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import model.Entry;

public class ListItemRenderer implements ListCellRenderer {

	private static int selectedIndex = -1;
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {

		// Eintrag holen.
		Entry e = (Entry) list.getModel().getElementAt(index);
		
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panel.setPreferredSize(new Dimension((int) list.getPreferredSize().getWidth(), 50));
		panel.add(new JLabel(e.getLabel()));
		
		panel.setBackground(Color.WHITE);
		if ( isSelected ) {
			panel.setBackground(Color.LIGHT_GRAY);
			
			if ( selectedIndex != index ) {
				selectedIndex = index;
				
				ItemList il = (ItemList) list;
				il.showEntry(e);
			}
		}
		
		return panel;
	}

}
